package cn.xz.gobang.activity

import androidx.recyclerview.widget.GridLayoutManager
import cn.xz.gobang.R
import cn.xz.gobang.adapter.ChessPAdapter
import cn.xz.gobang.base.BaseActivity
import cn.xz.gobang.bean.ChessPData
import cn.xz.gobang.bean.ClickData
import cn.xz.gobang.databinding.ActivityMnBinding
import cn.xz.gobang.dialog.BackDialog
import cn.xz.gobang.dialog.WinDialog
import cn.xz.gobang.util.ClickWinUtil
import cn.xz.gobang.util.ToastUtil
import cn.xz.gobang.util.gone
import cn.xz.gobang.util.visible

/**
 * 棋盘*/
class MnActivity: BaseActivity<ActivityMnBinding>(R.layout.activity_mn) {

    private var chessPAdapter: ChessPAdapter? = null

    private var mType = true //当前是否是黑棋，默认黑棋先下
    private var mClickData: ArrayList<ClickData> = arrayListOf() //已下棋子集合
    private var mClick: ChessPData? = null //当前下的棋子
    private var mIndex: Int = -1 //当前下的棋子的下标

    override fun initData() {
        val ms = object : GridLayoutManager(this, 15) {
            //禁止上下滑动
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        mDataBind.rvQp.layoutManager = ms
        chessPAdapter = ChessPAdapter()
        mDataBind.rvQp.adapter = chessPAdapter
        val mChess: ArrayList<ChessPData> = arrayListOf()
        for (i in 1..225){
            mChess.add(ChessPData())
        }
        chessPAdapter?.setList(mChess)
        chessPAdapter?.setOnItemClickListener { adapter, view, position ->
            val mData = chessPAdapter!!.data
            mClick = mData[position]
            if (mClick != null){
                //判断当前选位置是否有棋子,有棋子了，显示音效
                if (mClick!!.type == -1){
                    var clickType = 1
                    if (mType){
                        clickType = 0
                    }
                    mClick!!.type = clickType
                    chessPAdapter!!.notifyItemChanged(position)
                    mIndex = position
                    //显示是否落棋，禁止点击
                    mDataBind.viewUnClick.visible()
                    mDataBind.llcClick.visible()
                }else{
                    ToastUtil.show("已有棋子")
                }
            }
        }
    }


    override fun initEvent() {
        mDataBind.viewUnClick.setOnClickListener {  }
        //重新开始
        mDataBind.cvRestart.setOnClickListener {
            if (chessPAdapter != null){
                mClickData.clear()
                mDataBind.viewUnClick.gone()
                mDataBind.llcClick.gone()
                val mData = chessPAdapter!!.data
                mData.forEach {
                    it.type = -1
                }
                chessPAdapter!!.notifyDataSetChanged()
            }

        }
        //确认按钮
        mDataBind.llcOn.setOnClickListener {
            if (mClick != null && mIndex != -1){
                mType = !mType
                mClickData.add(ClickData(mClick!!.type,mIndex))

                mDataBind.viewUnClick.gone()
                mDataBind.llcClick.gone()

                //每次下完棋将数据存储起来
                ClickWinUtil.to(mClickData, chessPAdapter!!.data,object: ClickWinUtil.OnClickWinListener{
                    override fun success(type: Int) {
                        //有获胜方之后显示蒙层，禁止点击
                        mDataBind.viewUnClick.visible()
                        WinDialog.newInstance(type,-1).show(this@MnActivity.supportFragmentManager,"WinDialog123456")
                    }
                })
            }
        }
        //取消按钮
        mDataBind.llcCancel.setOnClickListener {
            val mData = chessPAdapter!!.data
            mData[mIndex].type = -1
            chessPAdapter!!.notifyItemChanged(mIndex)
            mDataBind.viewUnClick.gone()
            mDataBind.llcClick.gone()
        }
        //退出
        mDataBind.llcBack.setOnClickListener {
            finish()
        }
    }

}